package pikes.peak;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.web.servlet.support.RequestContext;

import pikes.html.xhtml.Flow;
import pikes.html.xhtml.list.ListItem;
import pikes.html.xhtml.list.UnorderedList;

public class ListComposite implements Component {

	private List<ComponentHolder> menuBar = new LinkedList<ComponentHolder>();

	public final Flow createHtml(Map model, RequestContext requestContext) throws Exception {

		LinkedList<ListItem> items = new LinkedList<ListItem>();

		for (ComponentHolder componentHolder : menuBar) {
			componentHolder.render(items, model, requestContext);
		}

		if (!items.isEmpty()) {
			UnorderedList ul = new UnorderedList(items.getFirst());

			Iterator<ListItem> iItems = items.iterator();
			iItems.next();

			while (iItems.hasNext()) {
				ul.add(iItems.next());
			}
			return ul;
		}
		else {
			UnorderedList ul = new UnorderedList("No content");
			return ul;
		}

	}

	public final void add(ComponentHolder holder) {
		menuBar.add(holder);
	}

	public final void add(Component component) {
		menuBar.add(new ComponentHolder(component));
	}

}
